package tech.zjp.ssm.service.impl;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tech.zjp.ssm.entity.Provider;
import tech.zjp.ssm.mapper.BillMapper;
import tech.zjp.ssm.mapper.ProviderMapper;
import tech.zjp.ssm.service.ProviderService;

/**
 *
 * @Description: 
 * @Author: 周军鹏
 * @Date: 2020年3月4日 下午2:35:26
 */
@Service
public class ProviderServiceImpl implements ProviderService {

	@Autowired
	private ProviderMapper providerMapper;
	@Autowired
	private BillMapper billMapper;
	
	@Override
	public List<Provider> getProviderList(String proName, String proCode, Integer pageNum, Integer pageSize)  {
		return providerMapper.getProviderList(proName, proCode, (pageNum-1)*pageSize, pageSize);
	}

	@Override
	public int getproviderCount(String proName, String proCode) {
		return providerMapper.getProviderCount(proName, proCode);
	}

	@Override
	public boolean add(Provider provider)  {
		return providerMapper.add(provider) > 0?true:false;
	}

	@Override
	public Provider getProviderById(Integer id)  {
		return providerMapper.getProviderById(id);
	}

	@Override
	public boolean modify(Provider provider)  {
		return providerMapper.modify(provider) > 0?true:false;
	}
	
	/**
	 * 业务：根据ID删除供应商表的数据之前，需要先去订单表里进行查询操作
	 * 若订单表中无该供应商的订单数据，则可以删除
	 * 若有该供应商的订单数据，则进行级联删除：先删订单，后删供应商
	 * 
	 */
	@Override
	public  boolean smbmsdeleteProviderById(Integer delId) {
		File upload = new File("D://upload//");
		boolean flag = true;
		int billCount = billMapper.getBillCountByProviderId(delId);
		if(billCount > 0 ){//先订单信息
			billMapper.deleteBillByProviderId(delId);
		}
		//先删除该条记录的上传文件
		Provider provider = providerMapper.getProviderById(delId);
		if(provider.getCompanyLicPicPath() != null && !provider.getCompanyLicPicPath().equals("")){
			//删除服务器上企业营业执照照片
			File file = new File(upload, provider.getCompanyLicPicPath());
			if(file.exists()){
				flag = file.delete();
			}
		}
		if(flag && provider.getOrgCodePicPath() != null && !provider.getOrgCodePicPath().equals("")){
			//删除服务器上组织机构代码证照片
			File file = new File(upload, provider.getOrgCodePicPath());
			if(file.exists()){
				flag = file.delete();
			}
		}
		if(flag){
			if(providerMapper.deleteProviderById(delId) < 1) {
				flag = false;
			}
		}
		return flag;
	}
	@Override
	public List<Provider> getProviderList()  {
		return providerMapper.getProList();
	}

}
